/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseImpersonationDao.cs
*        创建系统时间:       2012/11/2 19:06:59
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using System.Data;
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseImpersonationDao : BaseDao
    {
        public BaseImpersonationDao()
        {
            base.CurrentTableName = BaseStaffTable.TableName;
        }

        public BaseImpersonationDao(IDbHelper dbHelper)
            : this()
        {
            base.DbHelper = dbHelper;
        }

        public BaseImpersonationDao(BaseUserInfo userInfo)
            : this()
        {
            base.UserInfo = userInfo;
        }

        public BaseImpersonationDao(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            base.UserInfo = userInfo;
        }

        public DataTable GetAllowList()
        {
            string str2 = " SELECT " + BaseStaffTable.TableName + ".* ," + BaseOrganizeTable.TableName + "." + BaseOrganizeTable.FieldFullName + " AS DepartmentFullName ," + BaseItemDetailsTable.TableName + "." + BaseItemDetailsTable.FieldItemName + " AS DutyFullName ,OT.RoleFullName  FROM " + BaseStaffTable.TableName + "      LEFT OUTER JOIN " + BaseOrganizeTable.TableName + " ON " + BaseOrganizeTable.TableName + "." + BaseOrganizeTable.FieldID + " = " + BaseStaffTable.FieldDepartmentID + "      LEFT OUTER JOIN " + BaseItemDetailsTable.TableName + " ON  " + BaseItemDetailsTable.TableName + "." + BaseItemDetailsTable.FieldID + " = " + BaseStaffTable.FieldDutyID + "      LEFT OUTER JOIN (SELECT " + BaseUserTable.TableName + "." + BaseUserTable.FieldID + "," + BaseItemDetailsTable.TableName + "." + BaseItemDetailsTable.FieldItemName + " AS RoleFullName  FROM " + BaseUserTable.TableName + "," + BaseItemDetailsTable.TableName + " WHERE " + BaseUserTable.TableName + "." + BaseUserTable.FieldRole + " = " + BaseItemDetailsTable.TableName + "." + BaseItemDetailsTable.FieldID + ") OT                   ON " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldID + " = OT.ID  WHERE (" + BaseStaffTable.TableName + ".ID <> '" + base.UserInfo.ID + "')";
            string str3 = str2 + " AND " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldID + " IN (SELECT " + BaseDelegationTable.FieldDelegantID + " FROM " + BaseDelegationTable.TableName + " WHERE " + BaseDelegationTable.FieldConsignerID + "= '" + base.UserInfo.ID + "')";
            string sqlQuery = str3 + " ORDER BY " + BaseOrganizeTable.TableName + "." + BaseOrganizeTable.FieldSortCode + " ," + BaseStaffTable.TableName + "." + BaseStaffTable.FieldSortCode;
            DataTable dataTable = new DataTable(BaseStaffTable.TableName);
            base.DbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        public DataTable GetImpersonationList(string organizeID)
        {
            return this.GetImpersonationList(organizeID, string.Empty);
        }

        public DataTable GetImpersonationList(string organizeID, string search)
        {
            if (base.UserInfo.IsAdministrator)
            {
                BaseStaffDao dao = new BaseStaffDao(base.DbHelper, base.UserInfo);
                return dao.Search(organizeID, search, false);
            }
            search = BaseBusinessLogic.GetSearchString(search);
            string sqlQuery = " SELECT " + BaseStaffTable.TableName + ".* ," + BaseOrganizeTable.TableName + "." + BaseOrganizeTable.FieldFullName + " AS DepartmentFullName ," + BaseItemDetailsTable.TableName + "." + BaseItemDetailsTable.FieldItemName + " AS DutyFullName ,OT.RoleFullName  FROM " + BaseStaffTable.TableName + "      LEFT OUTER JOIN " + BaseOrganizeTable.TableName + " ON " + BaseOrganizeTable.TableName + "." + BaseOrganizeTable.FieldID + " = " + BaseStaffTable.FieldDepartmentID + "      LEFT OUTER JOIN " + BaseItemDetailsTable.TableName + " ON  " + BaseItemDetailsTable.TableName + "." + BaseItemDetailsTable.FieldID + " = " + BaseStaffTable.FieldDutyID + "      LEFT OUTER JOIN (SELECT " + BaseUserTable.TableName + "." + BaseUserTable.FieldID + "," + BaseRoleTable.TableName + "." + BaseRoleTable.FieldRealname + " AS RoleFullName  FROM " + BaseUserTable.TableName + "," + BaseRoleTable.TableName + " WHERE " + BaseUserTable.TableName + "." + BaseUserTable.FieldRole + " = " + BaseRoleTable.TableName + "." + BaseRoleTable.FieldID + ") OT                   ON " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldID + " = OT.ID  WHERE (" + BaseStaffTable.TableName + ".ID <> '" + base.UserInfo.ID + "')";
            if (!string.IsNullOrEmpty(organizeID))
            {
                string str2 = sqlQuery;
                string str3 = str2 + " AND (" + BaseStaffTable.FieldCompanyID + " = '" + organizeID + "'";
                sqlQuery = str3 + " OR " + BaseStaffTable.FieldDepartmentID + " = '" + organizeID + "')";
            }
            if (!string.IsNullOrEmpty(search))
            {
                string str4 = sqlQuery;
                string str5 = str4 + " AND (" + BaseStaffTable.FieldUsername + " LIKE '" + search + "'";
                string str6 = str5 + " OR " + BaseStaffTable.FieldRealname + " LIKE '" + search + "'";
                string str7 = str6 + " OR " + BaseOrganizeTable.TableName + "." + BaseOrganizeTable.FieldFullName + " LIKE '" + search + "'";
                sqlQuery = (str7 + " OR " + BaseItemDetailsTable.TableName + "." + BaseItemDetailsTable.FieldItemName + " LIKE '" + search + "'") + " OR OT.RoleFullName LIKE '" + search + "')";
            }
            if (!base.UserInfo.IsAdministrator)
            {
                string str8 = sqlQuery;
                sqlQuery = str8 + " AND " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldID + " IN (SELECT " + BaseDelegationTable.FieldConsignerID + " FROM " + BaseDelegationTable.TableName + " WHERE " + BaseDelegationTable.FieldDelegantID + " = '" + base.UserInfo.ID + "')";
            }
            string str9 = sqlQuery;
            sqlQuery = str9 + " ORDER BY " + BaseOrganizeTable.TableName + "." + BaseOrganizeTable.FieldSortCode + " ," + BaseStaffTable.TableName + "." + BaseStaffTable.FieldSortCode;
            DataTable dataTable = new DataTable(BaseStaffTable.TableName);
            base.DbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }
    }
}